'''
 * @Coding: UTF-8
 * @Author: GDNDZZK
 * @Date: 2023-05-16 15:45:53
 * @LastEditTime: 2023-05-16 16:08:09
 * @LastEditors: GDNDZZK
'''
from re import I
import urllib.request
import urllib.parse
from conf.utils import read_ini

setting = read_ini()
from urllib import request
import json


# chatGLM
def chatGLM(prompt="你好", history=[], isSetting=False):
    global setting
    if isSetting:
        setting = read_ini()

    # 创建一个Request对象
    req = request.Request(str(setting.get('API_GLM')), method="POST")
    # 添加头部信息
    req.add_header("Content-Type", "application/json")
    # 将要发送的数据转换为JSON格式，并编码为字节
    data = {"prompt": prompt,
            "history": history}
    data = json.dumps(data)
    data = data.encode()
    # 打开请求并读取响应内容
    r = request.urlopen(req, data=data)
    content = r.read()
    # content = content.decode('unicode-escape').encode('utf-16', 'surrogatepass').decode('utf-16')
    # print("output:", str(content))
    content = json.loads(content, strict=False)
    return content
